/*
   Author: mahaiyuan
   Date: 2021/8/25 8:20 下午
   Description:
*/
package router

import (
	"github.com/gin-gonic/gin"
	"go-web/internal/controller"
	"net/http"
)

func NewRouter() *gin.Engine {
	r := gin.New()

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"errNo": 404, "errMsg": "uri is not exist"})
	})

	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	tag := controller.NewTag()
	article := controller.NewArticle()

	apiv1 := r.Group("/api/v1")
	{
		apiv1.POST("/tags", tag.Create)
		apiv1.DELETE("/tags/:id", tag.Delete)
		apiv1.PUT("/tags/:id", tag.Update)
		apiv1.PATCH("/tags/:id/state", tag.Update)
		apiv1.GET("/tags", tag.List)

		apiv1.POST("/articles", article.Create)
		apiv1.DELETE("/articles/:id", article.Delete)
		apiv1.PUT("/articles/:id", article.Update)
		apiv1.PATCH("/articles/:id/state", article.Update)
		apiv1.GET("/articles/:id", article.Get)
		apiv1.GET("/articles", article.List)
	}

	return r
}
